#include "jecharts.h"
#include "Json/cJSON.h"
JECharts::JECharts(QObject *parent) : QObject(parent)
{
    tManager = TempManager::manager();
    title_text = QString("历史温度曲线").toUtf8();
    title_subtext = QByteArray();
    trigger = QByteArray("axis");
    showToolbox = true;
    echartRst = QByteArray();
    legendList.clear();
    devList.clear();
    devList = tManager->getTempRecordList();
    legendList = devList;
}

JECharts::~JECharts()
{
    qDebug()<<"JECharts delete";
}

QByteArray JECharts::getEchartJson(QDate startDate, QDate endDate)
{
    cJSON* rJson = cJSON_CreateObject();
    cJSON_AddItemToObject(rJson, "code", cJSON_CreateNumber(1));
    cJSON_AddItemToObject(rJson, "msg", cJSON_CreateString("success"));

    cJSON* eJson = cJSON_CreateObject();
    if(legendList.isEmpty())
    {
        cJSON_Delete(eJson);
        return QByteArray();
    }
    //set title
    cJSON* title = cJSON_CreateObject();
    cJSON_AddItemToObject(title, "text", cJSON_CreateString(title_text.data()));
    if(!title_subtext.isEmpty())
        cJSON_AddItemToObject(title, "subtext", cJSON_CreateString(title_subtext.data()));
    cJSON_AddItemToObject(eJson, "title", title);

    //set tooltip
    cJSON* tooltip = cJSON_CreateObject();
    cJSON_AddItemToObject(tooltip, "trigger", cJSON_CreateString(trigger.data()));
    cJSON_AddItemToObject(tooltip, "show", cJSON_CreateFalse());
    cJSON_AddItemToObject(eJson, "tooltip", tooltip);

    //set toolbox
    cJSON* toolbox = cJSON_CreateObject();
    cJSON_AddItemToObject(toolbox, "show", cJSON_CreateBool(showToolbox));
    cJSON* feature = cJSON_CreateObject();
    cJSON* mark = cJSON_CreateObject();
    cJSON* restore = cJSON_CreateObject();
    cJSON* saveAsImage = cJSON_CreateObject();
    cJSON_AddItemToObject(mark, "show", cJSON_CreateTrue());
    cJSON_AddItemToObject(restore, "show", cJSON_CreateTrue());
    cJSON_AddItemToObject(saveAsImage, "show", cJSON_CreateTrue());
    cJSON_AddItemToObject(feature, "mark", mark);
    cJSON_AddItemToObject(feature, "restore", restore);
    cJSON_AddItemToObject(feature, "saveAsImage", saveAsImage);
    cJSON_AddItemToObject(toolbox, "feature", feature);
    cJSON_AddItemToObject(eJson, "toolbox", toolbox);

    //set dataZoom
    cJSON* dataZoom = cJSON_CreateObject();
    cJSON_AddItemToObject(dataZoom, "show", cJSON_CreateTrue());
    cJSON_AddItemToObject(eJson, "dataZoom", dataZoom);

    //set legend
    cJSON* legend = cJSON_CreateObject();
    cJSON* legend_data = cJSON_CreateArray();
    foreach(QString legendStr, legendList)
    {
        QByteArray legendQba = legendStr.toUtf8();
        cJSON_AddItemToArray(legend_data, cJSON_CreateString(legendQba.data()));
    }
    cJSON_AddItemToObject(legend, "data", legend_data);
    cJSON_AddItemToObject(eJson, "legend", legend);

    //set xAxis
    cJSON* xAxis = cJSON_CreateArray();
    cJSON* xAxisItem = cJSON_CreateObject();
    cJSON_AddItemToObject(xAxisItem, "type", cJSON_CreateString("time"));
    cJSON_AddItemToObject(xAxisItem, "splitNumber", cJSON_CreateNumber(10));
    cJSON_AddItemToArray(xAxis, xAxisItem);
    cJSON_AddItemToObject(eJson, "xAxis", xAxis);

    //set yAxis
    cJSON* yAxis = cJSON_CreateArray();
    cJSON* yAxisItem = cJSON_CreateObject();
    cJSON_AddItemToObject(yAxisItem, "type", cJSON_CreateString("value"));
    cJSON_AddItemToArray(yAxis, yAxisItem);
    cJSON_AddItemToObject(eJson, "yAxis", yAxis);

    //set series
    int taskProgress = 0;
    int taskNum = devList.count();

    cJSON* series = cJSON_CreateArray();
    foreach(QString devStr, devList)
    {
        QString devName = devStr;
        int nameIndex = devList.indexOf(devStr);
        if(nameIndex < legendList.count())
            devName = legendList.at(nameIndex);

        cJSON* serie = cJSON_CreateObject();
        QByteArray legendQba = devName.toUtf8();
        cJSON_AddItemToObject(serie, "name", cJSON_CreateString(legendQba.data()));
        cJSON_AddItemToObject(serie, "type", cJSON_CreateString("line"));
        cJSON_AddItemToObject(serie, "showAllSymbol", cJSON_CreateFalse());
        cJSON* sData = cJSON_CreateArray();
        QVector<QVector2D> vData = tManager->getDeviceTempRecord(devStr, startDate, endDate);

        foreach(QVector2D d, vData)
        {
            cJSON* dataItem = cJSON_CreateArray();
            cJSON_AddItemToArray(dataItem, cJSON_CreateNumber(d.x()*1000));
            cJSON_AddItemToArray(dataItem, cJSON_CreateNumber(d.y()));
            cJSON_AddItemToArray(sData, dataItem);
        }
        vData.clear();
        vData.squeeze();
        cJSON_AddItemToObject(serie, "data", sData);
        cJSON_AddItemToArray(series, serie);
        taskProgress++;
        progress = taskProgress*100/taskNum-1;
        emit updateTaskProgress(progress);
    }
    cJSON_AddItemToObject(eJson, "series", series);
    cJSON_AddItemToObject(rJson, "data", eJson);
    echartRst = QByteArray(cJSON_Print(rJson));

    qDebug()<<echartRst;
    cJSON_Delete(rJson);
    progress = 100;
    emit updateTaskProgress(progress);
    return echartRst;
}

QByteArray JECharts::getEchartRst()
{
    return echartRst;
}

void JECharts::creatEchartJson(QDate startDate, QDate endDate)
{
    getEchartJson(startDate, endDate);
}
